#!/bin/bash - 
#===============================================================================
#
#          FILE: notify.sh
# 
#         USAGE: ./notify.sh 
# 
#   DESCRIPTION: notify by wechat to send tasks that should be reviewed 
#                today
# 
#       OPTIONS: ---
#  REQUIREMENTS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: YOUR NAME (), 
#  ORGANIZATION: 
#       CREATED: 02/02/2021 12:04
#      REVISION:  ---
#===============================================================================

set -o nounset                              # Treat unset variables as an error
#---  FUNCTION  ----------------------------------------------------------------#          NAME:  getWorkDir
#   DESCRIPTION:  get the directory where current shell program locates
#    PARAMETERS:
#       RETURNS:
#-------------------------------------------------------------------------------
getWorkDir()
{
    baseDir=$(dirname $0)
    if [ ${baseDir} = '.' ]; then
        echo "$(pwd)"
    elif [ ${baseDir:0:1} = '/' ]; then
        echo "${baseDir}"
    else
        echo "$(pwd)/${baseDir}"
    fi
}

WORKDIR=$(getWorkDir)
STORE_FILE="$WORKDIR/tasks.txt"

content=$(awk -F$'\t' '{
    if($4!="-"){
        nextreview=2 ^ $4 * 3600 * 24 + $5
        nexttime=strftime("%Y-%m-%d",nextreview);
        todaytime=strftime("%Y-%m-%d", systime());
        if(todaytime == nexttime){
            print "- " $2
        }    
    }
}' "$STORE_FILE")

curl --silent -G --data-urlencode "text=Review List" --data-urlencode "desp=$content" "https://sc.ftqq.com/SCU91812Te62a3057edac7569ed268df8a38ba0076016b3e21fc4c.send"

if [ ! $? -eq 0 ] ; then
    echo -e "\e[1;31mSend message failed!\e[0m"
fi
